package com.fwzc.giuseppe.activity;

import java.util.ArrayList;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;
import com.fwzc.giuseppe.R;
import com.fwzc.giuseppe.adapter.FragmentPageAdapter;
import com.fwzc.giuseppe.base.BaseActivity;
import com.fwzc.giuseppe.fragment.ReturnChangeFragment;
import com.fwzc.giuseppe.fragment.WaitCommentFragment;

public class ReceivedActivity extends BaseActivity implements OnClickListener,
OnPageChangeListener{
	private TextView wait_comment, return_change;
	private ViewPager viewPage;
	private ArrayList<Fragment> fragmentList = new ArrayList<Fragment>();
	private View slide_rod;
	private WindowManager manager;
	private int width, tabLength, currIndex;
	private FragmentPageAdapter adapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_received);
		
		initBar();
		initView();
		initViewPage();
	}
	
	private void initView() {
		actionbar_side_name.setText("已收货");
		iv_actionbar_side_left.setVisibility(View.VISIBLE);
		iv_actionbar_side_left.setOnClickListener(this);

		wait_comment = (TextView) findViewById(R.id.wait_commment);
		return_change = (TextView) findViewById(R.id.return_change);
		viewPage = (ViewPager) findViewById(R.id.match_content);
		slide_rod = (View) findViewById(R.id.slide_rod);
		wait_comment.setSelected(true);
		wait_comment.setOnClickListener(this);
		return_change.setOnClickListener(this);

		// 获取屏幕宽度，初始化滑块的宽度
		DisplayMetrics metrics = new DisplayMetrics();
		manager = this.getWindowManager();
		manager.getDefaultDisplay().getMetrics(metrics);
		width = metrics.widthPixels;
		tabLength = width / 2;
		LayoutParams lp = slide_rod.getLayoutParams();
		lp.width = tabLength;
		slide_rod.setLayoutParams(lp);
	}

	private void initViewPage() {
		WaitCommentFragment commentFragment = new WaitCommentFragment();
		ReturnChangeFragment return_change = new ReturnChangeFragment();
		fragmentList.add(commentFragment);
		fragmentList.add(return_change);
		adapter = new FragmentPageAdapter(getSupportFragmentManager(),
				fragmentList);
		viewPage.setAdapter(adapter);
		viewPage.setCurrentItem(0);
		viewPage.setOnPageChangeListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		// 返回
		case R.id.iv_actionbar_side_left:
			finish();
			break;

		// 待付款
		case R.id.wait_commment:
			viewPage.setCurrentItem(0);
			break;

		// 待收货
		case R.id.return_change:
			viewPage.setCurrentItem(1);
			break;
		default:
			break;
		}
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {

	}

	@Override
	public void onPageSelected(int position) {
		switch (position) {
		case 0:
			wait_comment.setSelected(true);
			return_change.setSelected(false);
			break;
			
		case 1:
			wait_comment.setSelected(false);
			return_change.setSelected(true);
			break;
		default:
			break;
		}
		Animation animation = new TranslateAnimation(tabLength * currIndex,tabLength * position, 0, 0);
		currIndex = position;
		animation.setFillAfter(true);
		animation.setDuration(300);
		slide_rod.startAnimation(animation);
	}
}
